from flask import request, jsonify

from api.apps.polish import polish_app
from api.models.bigmodels.ernie import ErniebotManager

from api import socketio

name_space = '/echo'
enumerate = ErniebotManager()
enumerate.initialize_erniebot()

# 当客户端连接时调用
@socketio.on('connect' , namespace=name_space)
def test_connect():
    print('client connected.')

# 当客户端断开连接时调用
@socketio.on('disconnect', namespace=name_space)
def test_disconnect():
    print('client disconnected.')

# 对问题进行提炼
@socketio.on('abstract', namespace=name_space)
def abstract(data):
    print('abstract',data)
    question = data
    answer = enumerate.abstract(question)
    print(answer)
    socketio.emit('abstract_response', {'answer':answer}, namespace=name_space)

# 续写
@socketio.on('constitute', namespace=name_space)
def constitute(data):
    print('constitute',data)
    question = data
    answer = enumerate.constitute(question)
    socketio.emit('constitute_response', {'answer':answer}, namespace=name_space)

# 对错误的句子进行处理
@socketio.on('errsentence', namespace=name_space)
def constitute(data):
    print('errsentence',data)
    question = data
    answer = enumerate.errsentence(question)
    print(answer)
    socketio.emit('errsentence_response', {'answer':answer}, namespace=name_space)

# 对问题进行润色
@socketio.on('polish', namespace=name_space)
def constitute(data):
    print('polish',data)
    question = data
    answer = enumerate.polish(question)
    socketio.emit('polish_response', {'answer':answer}, namespace=name_space)

# 对问题进行翻译
@socketio.on('translate', namespace=name_space)
def constitute(data):
    print('translate',data)
    question = data
    answer = enumerate.translate(question)
    socketio.emit('translate_response', {'answer':answer}, namespace=name_space)

# 当访问 '/index' 时调用
@polish_app.route('/index',methods=["GET"])
def index():
    return "启动polish成功！"


# @polish_app.route('/abstract',methods=['POST'])
# def translate():
#     data = request.get_json()
#     if data:
#         type = data.get('type')
#         question = data.get('question')
#
#         erniebot = ErniebotManager()
#         erniebot.initialize_erniebot()
#
#         if type == 'abstract':
#             result = erniebot.abstract(question)
#             return jsonify(result)
#     else:
#         return jsonify({'error': 'Invalid JSON data'})